package cn.itcast.mq;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: SoringAmqpTest
 * Package: cn.itcast.mq
 * Description:
 *
 * @Author BuTang
 * @Create 2023/12/12 9:51
 * @Version 1.0
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringAmqpTest {
    @Autowired
    private RabbitTemplate rabbitTemplate ;
    @Test
    public void testSimpleQueue(){

        Map<String,Object> msg = new HashMap<>();
        msg.put("name", "Jack");
        msg.put("age", 21);
        // 队列名称
        String queueName = "simple.queue";
        // 消息
        String message = "hello, spring amqpaaaaaaaaaaaa!";
        // 发送消息
        rabbitTemplate.convertAndSend(queueName,msg);
    }


    @Test
    public void testFanoutExchange(){

        int a = 0;
        while (a<100) {
            a++;
            // 队列名称
            String exchangeName = "itcast.fanout";
            // 消息
            String message = "hello, everyone!";
            rabbitTemplate.convertAndSend(exchangeName, "", message);
        }
    }

    @Test
    public void testSendDirectExchange() {
        // 交换机名称
        String exchangeName = "itcast.direct";
        // 消息
        String message = "red";
        // 发送消息
        rabbitTemplate.convertAndSend(exchangeName, "red", message);
    }


    /**
     * topicExchange
     */
    @Test
    public void testSendtTopicExchange() {
        // 交换机名称
        String exchangeName = "itcast.topic";
        // 消息
        String message = "马上就要回家啦";
        // 发送消息
        rabbitTemplate.convertAndSend(exchangeName, "1111.news", message);
    }


}
